<template>
	<view class="wrap">
		<view class="head">
			<uni-state></uni-state>
			<navigator class="close" url="../user/user" open-type="switchTab">
				关闭
			</navigator>
		</view>
		<view class="main">
			<view class="logo">
				蓝米
			</view>
			<view class="info">
				<input type="text" placeholder="请输入用户名" v-model="username" >
				<input type="text" placeholder="请输入密码" v-model="password" >
				<view class="txt">
					<checkbox-group @change="handleChange">
						<label>
							<checkbox value="1"></checkbox><text>同意协议</text>
						</label>
					</checkbox-group>
					
				</view>
				<view class="login" @click="handleLogin">
					登录
				</view>
				<view class="signTxt">
					没有账号?
					<navigator url="/pages/sign/sign">去注册</navigator>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username:"",
				password:"",
				checked:[],
				userList:[]
			}
		},
		created() {
			if(uni.getStorageSync("userList")){
				this.userList = uni.getStorageSync("userList")
			}else{
				this.userList = []
			}
		},
		methods: {
			handleChange(e){
				console.log(e.detail.value)
				this.checked = e.detail.value
			},
			handleLogin(){
				if(this.checked.length){
					//登录逻辑
					//1、先判断用户名是否存在，如果存在，给一个为真的状态的，并且记录改数据的索引
					let userSate = false
					let index = 0
					this.userList.forEach((v,i)=>{
						if(v.username == this.username){
							userSate = true
							index = i 
						}
					})
					if(userSate){
						if(this.userList[index].password == this.password){
							uni.setStorageSync("loginInfo",this.userList[index].username)
							uni.showToast({
								title:"登录成功"
							})
							setTimeout(()=>{
								uni.switchTab({
									url:"../user/user"
								})
							},2000)
						}else{
							uni.showToast({
								title:"密码错误",
								icon:"error"
							})
						}
					}
				}else{
					uni.showToast({
						title:"请勾选协议！",
						icon:"error"
					})
				}
			}
		}
	}
</script>

<style lang="less">
	.wrap{
		.head{
			padding: 0 30rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
		}
		.main{
			padding: 30rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			.logo{
				font-size: 88rpx;
				font-weight: bold;
				color:skyblue;
				margin-top: 60rpx;
			}
			.info{
				margin-top: 60rpx;
				input,.login{
					width: 600rpx;
					height: 80rpx;
					border: 2rpx solid gray;
					border-radius: 40rpx;
					margin: 20rpx 0;
				}
				input{
					padding: 0 30rpx;
				}
				.login{
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: skyblue;
					color: white;
					border: none;
				}
				.signTxt{
					text-align: center;
					navigator{
						color:skyblue;
						text-decoration: underline;
					}
				}
				.txt{
					text{
						
					}
				}
			}
		}
	}
</style>
